import java.util.Arrays;
import java.util.NoSuchElementException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:岁亮亮
 * Date:2024-05-28
 * TIME:13:55
 */
public class MyStack implements StackList{

    private int[] elementData;
    private int usedSize;
    private static final int DEAFAULT_CAPACITY = 10;
    public MyStack(){
        elementData = new int[DEAFAULT_CAPACITY];
    }

    @Override
    public void push(int target) {
        checkCapacity(elementData);
        elementData[usedSize++] = target;
    }

    private void checkCapacity(int[] elementData) {
        if(isFull()){
            elementData = Arrays.copyOf(elementData,elementData.length*2);
        }
    }
    private boolean isFull(){
        return usedSize == elementData.length;
    }

    @Override
    public int peek() {
       if(isEmpty()){
           throw new NoSuchElementException();
       }
       return elementData[usedSize-1];

    }

    @Override
    public int pop() {
       int target = peek();
       usedSize--;
       return target;
    }

    @Override
    public boolean isEmpty() {
        return usedSize == 0;
    }

    @Override
    public int size() {
        return usedSize;
    }
}
